var express = require('express');
var router = express.Router();
const shsDB = require("./../../db/index.js");
// 推荐好友
router.post("/list",jwtVerify, function(req, res) {
	let obj = {
		code: 201,
		message: "",
		data: {
			message: "", //业务消息
		}
	}
	try {
		let Item = req.body;
		let userId = req.shsImUserInfo.fromId
		let pageSizes = Item.pageSizes || 10; //条数
		let pageNum = Item.pageNum || 1; //第几页
		let pageStart = pageSizes * (pageNum - 1); //pageSizes*(pageNum-1)
		let sql =
			`select users.Id, users.username,users.url,users.gender,users.Integral,users.createTime,users.hobbylist,users.signature from users where Id not in (select otherId from relationshipuser where userId='${userId}') and Id !='${userId}' and status=1  ORDER BY Integral desc,createTime asc limit ${pageStart},${pageSizes};`

		if (Item.code == 'like') {
			let hobbys =typeof Item.hobbys =='string'? Item.hobbys.split(","):Item.hobbys;
			let hobbysLike = hobbys[0] ? `hobbylist like '%${hobbys[0]}%'` :"hobbylist like ''"
			for (let i = 1; i < hobbys.length; i++) {
				hobbysLike += `or hobbylist like '%${hobbys[i]}%'`
			}
			sql =
				`select users.Id, users.username,users.url,users.gender,users.Integral,users.createTime,users.hobbylist,users.signature from users where Id not in (select otherId from relationshipuser where userId='${userId}') and Id !='${userId}' and (${hobbysLike})  and status=1  ORDER BY Integral desc,createTime asc limit ${pageStart},${pageSizes};`
		}
		shsDB(sql).then(data => {
			obj.code = 200
			obj.message = "success"
			obj.data = data
			res.send(JSON.stringify(obj))
		}).catch(err => {
			obj.message = err.message;
			res.send(JSON.stringify(obj))
		})
	} catch (e) {
		obj.message = e.message;
		res.send(JSON.stringify(obj))
	}

})
module.exports = router;